package com.yutian.selfservice.support.utils;

import com.github.pagehelper.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class Result extends HashMap<String, Object> {

    public static final String CODE_TAG = "code";

    public static final String MSG_TAG = "msg";

    public static final String DATA_TAG = "data";
    public static final String TOTAL_TAG = "total";

    public static Result successPage(Page list) {
        return successPage(list, list.getTotal());
    }
    public static Result successPage(List list, Long total) {
        Result result = Result.success(list);
        result.put(TOTAL_TAG,total);
        return result;
    }

    /**
     * 状态类型
     */
    public enum Type
    {
        SUCCESS("00"),
        FAILED("99"),
        PARAM_ERROR("10");
        private final String value;

        Type(String value)
        {
            this.value = value;
        }

        public String value()
        {
            return this.value;
        }
    }

    public Result()
    {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param type 状态类型
     * @param msg 返回内容
     */
    public Result(Type type, String msg)
    {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
    }

    public Result(String type, String msg, Object data)
    {
        super.put(CODE_TAG, type);
        super.put(MSG_TAG, msg);
//        if (!Objects.isNull(data))
//        {
//            super.put(DATA_TAG, data);
//        }
        super.put(DATA_TAG, data);
    }

    public static Result success()
    {

        return Result.success(null);
    }

    public static Result success(Object data)
    {

        return success("操作成功", data);
    }

    public static Result success(String msg, Object data)
    {
        return new Result(Type.SUCCESS.value, msg, data);
    }
    public static Result fail(String msg, Object data)
    {
        return new Result(Type.FAILED.value, msg, data);
    }
    public static Result fail(String msg, String code,Object data)
    {
        return new Result(code, msg, data);
    }
    public static Result fail(String msg)
    {
        return new Result(Type.FAILED.value, msg, null);
    }
    public static Result paramError(String msg)
    {
        return new Result(Type.PARAM_ERROR.value, msg, null);
    }

    public boolean isSuccess(){
        return Type.SUCCESS.value.equals(String.valueOf(this.getOrDefault(CODE_TAG, -1)));
    }
    public Object getData(){
        return this.get(DATA_TAG);
    }

    public  Result setItem(String key,Object value){
        this.put(key, value);
        return this;
    }
    public boolean isParamError(){
        return Type.PARAM_ERROR.value.equals(String.valueOf(this.getOrDefault(CODE_TAG, -1)));    }
}

